package org.idea.spring.framework.http.util;

import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.StreamUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;

/**
 * @Author linhao
 * @Date created in 9:09 下午 2021/5/22
 */
public class HttpRequestFactoryDemo {

    public static void main(String[] args) throws URISyntaxException, IOException {
        ClientHttpRequestFactory chrf = new SimpleClientHttpRequestFactory();
        ClientHttpRequest clientHttpRequest = chrf.createRequest(new URI("http://www.baidu.com"), HttpMethod.GET);
        ClientHttpResponse clientHttpResponse = clientHttpRequest.execute();
        InputStream inputStream = clientHttpResponse.getBody();
        String response = StreamUtils.copyToString(inputStream, Charset.forName("UTF-8"));
        inputStream.close();
        System.out.println(response);
    }
}
